package com.alibaba.dubbo.common.io;

import java.io.Serializable;
import java.nio.ByteBuffer;

public class ByteArray implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	byte []data;
	int size;
	int offset;
	public byte[] data() {
		return data;
	}
	 
	public int size() {
		return size;
	}
	 
	public int offset() {
		return offset;
	}
	 
	
	public ByteArray(byte[] data){
		this(data, 0, data.length);
	}
	
	public ByteArray(byte[] data, int size){
		this(data, 0, size);
	}
	
	public ByteArray(byte[] data, int offset, int size){
		if(data == null || offset < 0 || size < 0 || (offset >= data.length && data.length == 0)
				|| offset + size > data.length) {
			throw new IndexOutOfBoundsException("offset=" + offset + ", size=" + size);
		}
		this.data = data;
		this.offset = offset;
		this.size = size;
	}
	
	public byte[] toBytes(){
		byte[]data = new byte[size];
		System.arraycopy(this.data, 0, data, 0, this.size);
		return data;
	}
	
	public ByteBuffer toByteBuffer(){
		return ByteBuffer.wrap(data, 0, size);
	}
	
	@Override
	public ByteArray clone(){
		return new ByteArray(this.data, this.offset, this.size);
	}
}
